package com.whlilong.jxc.editor.support;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;

import com.whlilong.jxc.listener.IEditorListener;
import com.whlilong.jxc.listener.ITextKeyChangeListener;
import com.whlilong.jxc.model.Hsinfo;
import com.whlilong.jxc.model.Keshang;
import com.whlilong.jxc.model.Kstype;
import com.whlilong.jxc.sys.CnToSpellFirstChar;
import com.whlilong.jxc.sys.ExtentionInfo;

public class BussinessInfoEditorSupport extends EditingSupport implements
		ITextKeyChangeListener {

	/**
	 * 当前第几列
	 */
	private int currentColumn;

	/**
	 * 是否第一次修改该单元格
	 */
	private boolean isFirst = true;

	/**
	 * 为单元格添加的editor
	 */
	private CellEditor editor;

	/**
	 * 货商类别list
	 */
	private List<Kstype> merchantTypelist = new ArrayList<Kstype>();

	/**
	 * 货商区域list
	 */
	private List<Keshang> districtList = new ArrayList<Keshang>();
	
	@SuppressWarnings("unchecked")
	public BussinessInfoEditorSupport(TableViewer viewer, int column,
			IEditorListener listener) {
		super(viewer);
		merchantTypelist = ExtentionInfo.getInstance().getKstypeinfo();
		String[] merchantItems = new String[merchantTypelist.size() + 1];
		merchantItems[0] = "不定";
		for (int i = 0; i < merchantTypelist.size(); i++) {
			merchantItems[i + 1] = merchantTypelist.get(i).getKstype();
		}

		districtList = ExtentionInfo.getInstance().getKeshanginfo();
		String[] districtItems = new String[districtList.size() + 1];
		districtItems[0] = "不定";
		for (int i = 0; i < districtList.size(); i++) {
			districtItems[i + 1] = districtList.get(i).getKeshangarea();
		}

		this.currentColumn = column;
		switch (column) {
		case 2:
		case 3:
		case 7:
		case 8:
		case 9:
		case 10:
		case 11:
		case 12:
		case 13:
			editor = new BussinessInfoTextKeyCellEditor(viewer.getTable(),
					listener, column, this);
			break;
		case 5:
			editor = new BusinessInfoComboBoxCellEditor(viewer.getTable(),
					listener, column, this, districtItems);
			break;
		case 6:
			editor = new BusinessInfoComboBoxCellEditor(viewer.getTable(),
					listener, column, this, merchantItems);
			break;
		default:
			editor = null;
		}

	}

	@Override
	protected boolean canEdit(Object element) {
		// TODO Auto-generated method stub
		return (currentColumn != 1 && currentColumn != 4);
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		// TODO Auto-generated method stub
		return editor;
	}

	@Override
	protected Object getValue(Object element) {
		Hsinfo obj = (Hsinfo) element;
		switch (currentColumn) {
		case 2:
			String id = obj.getHsid();
			if (isFirst) {
				((BussinessInfoTextKeyCellEditor) editor).setOldString(id);
				isFirst = false;
			}
			return id;
		case 3:
			return obj.getHsname();
//		case 4:
//			return obj.getPym();
		case 5:
			for (int i = 0; i < districtList.size(); i++)
				if (obj.getHsareaname()==districtList.get(i).getKeshangarea())
					return i+1;
			return 0;
		case 6:
			for (int i = 0; i < merchantTypelist.size(); i++)
				if (obj.getHstpname()==merchantTypelist.get(i).getKstype())
					return i + 1;
			return 0;
		case 7:
			return obj.getHstel();
		case 8:
			return obj.getHsaddress();
		case 9:
			return obj.getPeople();
		case 10:
			return obj.getFax();
		case 11:
			return obj.getYzbm();
		case 12:
			return obj.getMail();
		case 13:
			return obj.getYh();
		}

		return null;
	}

	@Override
	protected void setValue(Object element, Object value) {
		Hsinfo obj = (Hsinfo) element;
		switch (currentColumn) {
		case 2:
			obj.setHsid(String.valueOf(value));
			break;
		case 3:
			obj.setHsname(String.valueOf(value));
			break;
//		case 4:
//			obj.setPym(String.valueOf(value));
//			break;
		case 5:
			if (Integer.valueOf(String.valueOf(value)) == 0)
				obj.setHsareaname("不定");
			for (int i = 0; i < districtList.size(); i++)
				if (Integer.valueOf(String.valueOf(value)) == i+1)
					obj.setHsareaname(districtList.get(i).getKeshangarea());
			break;
		case 6:
			if (Integer.valueOf(String.valueOf(value)) == 0)
				obj.setHstpname("不定");
			for (int i = 0; i < merchantTypelist.size(); i++)
				if (Integer.valueOf(String.valueOf(value)) == i + 1)
					obj.setHstpname(merchantTypelist.get(i).getKstype());
			break;
		case 7:
			obj.setHstel(String.valueOf(value));
			break;
		case 8:
			obj.setHsaddress(String.valueOf(value));
			break;
		case 9:
			obj.setPeople(String.valueOf(value));
			break;
		case 10:
			obj.setFax(String.valueOf(value));
			break;
		case 11:
			obj.setYzbm(String.valueOf(value));
			break;
		case 12:
			obj.setMail(String.valueOf(value));
			break;
		case 13:
			obj.setYh(String.valueOf(value));
			break;
		}
		getViewer().update(element, null);
	}

	@Override
	public void changeEvent() {
		if (editor instanceof BussinessInfoTextKeyCellEditor)
			((BussinessInfoTextKeyCellEditor) editor).releaseKey();
		if (editor instanceof BusinessInfoComboBoxCellEditor)
			((BusinessInfoComboBoxCellEditor) editor).releaseKey();
	}

	@Override
	public void modify() {
		isFirst = true;
	}

}